//
//  DuMainViewController.swift
//  WeiBo
//
//  Created by Du on 15/9/28.
//  Copyright © 2015年 Du. All rights reserved.
//

import UIKit

class DuMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.tintColor = UIColor.orangeColor()
        //添加子控制器
        addChildViewController()
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //添加加号按钮
        tabBar.addSubview(composeButton)
        //计算宽高
        let width = UIScreen.mainScreen().bounds.width / CGFloat( childViewControllers.count)
        let rect = CGRect(origin: CGPointZero, size: CGSizeMake(width, 49))
        composeButton.frame = CGRectOffset(rect, 2 * width, 0)
    }
    
    func addChildViewController() {
        do{
            //加载json文件
            let jsonPath = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
            //转换为NSData对象
            let data = NSData(contentsOfFile: jsonPath)!
            //创建NSData数组
            let dictArray = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)
            //遍历数组
            for dict in dictArray as! [[String: AnyObject]] {
                //添加子控制器
                addChildViewController(dict["vcName"] as! String, imageName: dict["imageName"] as! String, title: dict["title"] as! String)
            }
        }catch{
            addChildViewController("DuHomeViewController", imageName: "tabbar_home", title: "首页")
            addChildViewController("DuMessageViewController", imageName: "tabbar_message_center", title: "消息")
            addChildViewController("NullViewController", imageName: "", title: "")
            addChildViewController("DuDiscoverViewController", imageName: "tabbar_discover", title: "广场")
            addChildViewController("DuProfileViewController", imageName: "tabbar_profile", title: "我")
        }
    }
    /**
    初始化子控制器
    */
    func addChildViewController(vcName:String, imageName:String, title:String){
        //获取命名空间
        let nsp = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        // 注意: 在Swift中, 如果想通过一个字符串创建一个类, 那么必须加上命名空间, 否则创建出来是nil
        let myClass: AnyClass = NSClassFromString(nsp + "." + vcName)!
        //AnyClass并不知道真实的类型，所以需要强制转换
        let vc = (myClass as! UIViewController.Type).init()
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        vc.title = title
        //包装成导航
        let navigation = UINavigationController()
        navigation.addChildViewController(vc)
        addChildViewController(navigation)
    }
    
    //懒加载
       private lazy var composeButton:UIButton = {
        //1、创建
        let btn = UIButton()
        //2、图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        // 3.设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //监听
        btn.addTarget(self, action: Selector("composeBtnClick"), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    
    func composeBtnClick() {
        print(__FUNCTION__)
    }
    
}
